<template>
  <div class="content">
    <datatable-server :columns="columns" @loaded="onDataLoad" url="http://beta-redis-q.kuaiyudian.com/quotations-vt"></datatable-server>
  </div>
</template>
<script>
import {
  DatatableServer
} from 'components/Datatables'
// import socketIO from 'socket.io-client'
// import {
//   strToQuotation
// } from 'libs/utils'
export default {
  components: {
    DatatableServer
  },
  created: function () {
    console.log('created')
      // this._socket = socketIO('http://beta-redis-q.kuaiyudian.com')
  },
  destroyed: function () {
    console.log('destroyed')
      // this._socket.removeListener('quotation', this._subsriber)
      // this._socket.disconnect()
  },
  data: function () {
    return {
      _socket: null,
      columns: [
        'symbol', {
          name: 'stockName',
          title: '股票名称',
          emptyAsNull: true,
          orderable: false,
          componentable: true,
          data: function () {
            return {
              template: `<router-link to="buttons">${this.stockName}</router-link>`
            }
          }
        }, {
          name: 'openPrice',
          title: '今开',
          searchable: false
        }, {
          name: 'preClosePrice',
          title: '昨收',
          searchable: false
        }, {
          name: 'highPrice',
          title: '最高',
          searchable: false
        }, {
          name: 'lowPrice',
          title: '最低',
          searchable: false
        }, {
          name: 'nowPrice',
          searchable: false,
          title: '最新价格'
        }, {
          name: 'volume',
          searchable: false,
          title: '成交量'
        }, {
          name: 'amount',
          searchable: false,
          title: '成交额'
        }
      ]
    }
  },
  methods: {
    onDataLoad: function (rowData, datatable) {
      // const symbols = rowData.map((r) => r.symbol)
      // this._socket.emit('listen-stock', symbols.join('|'))
      // this._subsriber = function (s) {
      //   let q = strToQuotation(s)
      //   rowData.forEach(function (row) {
      //     if (row.symbol === q.symbol) {
      //       row = Object.assign(row, q)
      //     }
      //   })
      // }
      // this._socket.on('quotation', this._subsriber)
      // datatable.$on('loading', () => {
      //   this._socket.removeListener('quotation', this._subsriber)
      // })
    }
  }
}
</script>
<style lang="scss" scoped>
.is-title {
  text-transform: capitalize;
}
</style>
